﻿using System;
using System.Web.Security;
using System.Web.UI;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design
{
    public partial class LogOn : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the LogOnButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LogOnButton_Click(object sender, EventArgs e)
        {
            string account = UserName.Text;
            string password = Password.Text;

            ValidateUserResult result = AccountBO.ValidateUser(account, password);
            if (result == ValidateUserResult.Success)
            {
                string returnUrl = Request.Params["ReturnUrl"];

                User user = AccountBO.GetUserByAccountName(account);
                Session["userId"] = user.UserId;

                if (!string.IsNullOrEmpty(returnUrl))
                {
                    FormsAuthentication.RedirectFromLoginPage(account, true);
                }
                else
                {
                    FormsAuthentication.RedirectFromLoginPage(account, true);
                    Response.Redirect("~/Index.aspx");
                }
            }
            else if (result == ValidateUserResult.IsInactive)
            {
                InformMessage.Text = "User is inactivated !";
            }
            else
            {
                InformMessage.Text = "Username or Password do not match !";
            }
        }

        /// <summary>
        /// Handles the Click event of the ForgotPasswordButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ForgotPasswordButton_Click(object sender, EventArgs e)
        {
        }
    }
}